<?php
/**
 * Created by PhpStorm.
 * User: zrc
 * Date: 2018/3/26
 * Time: 11:28
 */

namespace App\Http\Controllers\Coin;


use App\Http\Controllers\Controller;
use App\Library\Yunxin;
use App\Traits\YunXinTrait;

class YnxinController extends Controller
{
    use YunXinTrait;

    public function test(){
        $a = $this->createUserIds(1,'测试1','','',123456,1);
        var_dump($a);
//        $y = new Yunxin\Yunxin();
//        $a = $y->test();
       // dump($a);
    }

    public function check_yx(){
        $model = new Yunxin\Yunxin();
        $arr = $model->check_yx(1);
        return $arr;
    }

    public function register_yx(){
        $id=2;
        $name='测试2';
        $img='www.baidu.com';
        $model = new Yunxin\Yunxin();
        $arr = $model->register_yx($id,$name,$img);
        return $arr;
    }

    public function send_yx_msg(){
        $id1=1;
        $id2=2;
        $msg1='测试1';
        $msg2='测试2';

//        $msg1=json_encode('测试1');
//        $msg2=json_encode('测试2');
        $model = new Yunxin\Yunxin();
        $arr = $model->send_yx_msg($id1,$id2,$msg1,$msg2);
        return $arr;
    }

    public function lock_yx_msg(){
        $model = new Yunxin\Yunxin();

        $arr = $model->lock_yx_msg();
        return $arr;

    }
    public function change_yx(){
        $model = new Yunxin\Yunxin();
        $arr = $model->change_yx(1,'哈哈','www.baidu.com');
        return $arr;

    }
    /**
     * @author: zrc
     * @access: 云信计划任务
     * @param: variable
     * @ date:  2018年1月3日
     * @return :
     */
    public function yx_add()
    {
        $yx_model = new \app\common\model\Yunxin();
        $list = Db::name('user')->order('userID asc')->where('yx_status',1)->limit(50)->select();
        if (count($list)<1){
            puterror(2);
        }
        foreach ($list as $k=>$v){
            if ($v['headimg_oss']&&$v['headimg_oss']!='oss'){
                $icon=joinOssImg($v['headimg_oss']);
            }else{
                $icon=config('weburl').$v['headimg'];
            }
            if ($v['type']==2){
                $token = $v['token2'];
            }else{
                $token = $v['token1'];
            }
            //是否实名认证
            $where11['userID'] =  $v['userID'];
            $autonym = Db::name('autonym')->where($where11)->value('name')??'';
            $arr_yx = [
                'accountName'   =>  $v['account'],
                'realname'      =>  $autonym
            ];
            $json_yx =json_encode($arr_yx);
            $yx_register = $yx_model->createUserIds($v['userID'], $v['nickname'],$json_yx,$icon,$token,$v['sex']);
            if (count($yx_register)>0){
                if ($yx_register['code'] == 200){
                    $re = Db::name('user')->where('userID',$v['userID'])->update(['yx_status'=>2]);
                    if($re!==false){
                        $data['s'][]=$v['userID'];//注册成功
                        //记录错误信息
                        \SeasLog::setBasePath(ROOT_PATH.'log');
                        \SeasLog::setLogger('yx_s');
                        \SeasLog::info($v['userID'].'注册成功');
                    }else{
                        $data['ee'][]=$v['userID'];//注册成功云信注册状态更改失败
                        //记录错误信息
                        \SeasLog::setBasePath(ROOT_PATH.'log');
                        \SeasLog::setLogger('yx_ee');
                        \SeasLog::error($v['userID'].'注册成功云信注册状态更改失败');
                    }
                }else{
                    if ($yx_register['desc']=='already register'){
                        $re = Db::name('user')->where('userID',$v['userID'])->update(['yx_status'=>2]);
                        $data['ss'][]=$v['userID'];//已经注册了
                        //记录错误信息
                        \SeasLog::setBasePath(ROOT_PATH.'log');
                        \SeasLog::setLogger('yx_ss');
                        \SeasLog::info($v['userID'].'已经注册了');
                    }else{
//                        dump($yx_register);
//                        die;
                        $data['e'][]=$v['userID'];//注册其他问题
                    }
                }
            }else{
                $data['eee'][]=$v['userID'];//请求注册云信url没返回
            }

        }
        putsuccess($data);
    }


}